package swings.panels;

import swings.labels.LimpidLabel;
import swings.others.CommonJTextArea;
import swings.others.CommonScrollPane;
import swings.others.LineNumberHeaderView;
import utils.CommonUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class BodyPanel extends CommonPanel{

    public CommonJTextArea textArea = new CommonJTextArea(10,25);

    public BodyPanel(){
        CommonScrollPane bodyScroll = new CommonScrollPane(2,2,WorkPanel.width-10,WorkPanel.height/2-85);
        bodyScroll.getViewport().add(textArea);
        bodyScroll.setRowHeaderView(new LineNumberHeaderView());
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        textArea.setFont(CommonUtils.getFont());
        textArea.setTabSize(4);
        textArea.setLineWrap(true);
        textArea.setForeground(Color.WHITE);
        textArea.setBounds(0,0,WorkPanel.width-10,WorkPanel.height/2-80);
        textArea.setPreferredSize(new Dimension(WorkPanel.width-10,600));
        textArea.setOpaque(false);
        LimpidLabel textBg = new LimpidLabel(ImageUtils.common,0,0,WorkPanel.width-10,WorkPanel.height/2-80);
        textBg.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, false));
        this.add(bodyScroll);
        this.setBounds(5,81,WorkPanel.width,WorkPanel.height/2-80);
//        this.add(new LimpidLabel(ImageUtils.common,0, 0, WorkPanel.width, WorkPanel.height/2-80));
        this.add(textBg);
        this.setBorder(null);
    }

    public BodyPanel(String data){
        CommonScrollPane bodyScroll = new CommonScrollPane(2,2,WorkPanel.width-10,WorkPanel.height/2-55);
        bodyScroll.getViewport().add(textArea);
        bodyScroll.setRowHeaderView(new LineNumberHeaderView());
        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        textArea.setFont(CommonUtils.getFont());
        textArea.setTabSize(4);
        textArea.setLineWrap(true);
        textArea.setForeground(Color.WHITE);
        textArea.setBounds(0,0,WorkPanel.width-10,WorkPanel.height/2-50);
        textArea.setPreferredSize(new Dimension(WorkPanel.width-10,600));
        textArea.setOpaque(false);
        textArea.setEditable(false);
        LimpidLabel textBg = new LimpidLabel(ImageUtils.common,0,0,WorkPanel.width-10,WorkPanel.height/2-50);
        textBg.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, false));
        this.add(bodyScroll);
        this.setBounds(5,WorkPanel.height/2 + 46,WorkPanel.width-10,WorkPanel.height/2+20);
//        this.add(new LimpidLabel(ImageUtils.common,0, 0, WorkPanel.width, WorkPanel.height/2-50));
        this.add(textBg);
        this.setBorder(null);
    }

    public void setData(String data){
        if (null != data && !data.equals("")){
            int sum = data.length()-data.replaceAll("\n\t", "").length();
            if (sum<10 && data.length()>300){
                sum = data.length()/35;
            }
            textArea.setRows(sum * 13/20);
            textArea.setText(data);
        }else {
            textArea.setText("");
        }
    }

}
